package com.snow.useviewdemo.qiniuplay.video;

import android.content.Context;

import com.snow.common.service.NetworkUtil;
import com.snow.common.tool.utils.ToastUtils;
import com.snow.useviewdemo.APPApplication;
import com.snow.useviewdemo.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019/1/2
 * desc   : 播放器相关 utils 处理类
 */


public class PlayFileUtils {
    private static final String SEPARATOR = File.separator;//路径分隔符

    private static PlayFileUtils fileUtils;

    public static PlayFileUtils init() {
        if (fileUtils == null) {
            synchronized (PlayFileUtils.class) {
                if (fileUtils == null) {
                    fileUtils = new PlayFileUtils();
                }
            }
        }
        return fileUtils;
    }


    /**
     * 复制res/raw中的文件到指定目录
     *
     * @param context     上下文
     * @param id          资源ID
     * @param fileName    文件名
     * @param storagePath 目标文件夹的路径
     */
    public static void copyFilesFromRaw(Context context, int id, String fileName, String storagePath) {
        InputStream inputStream = context.getResources().openRawResource(id);
        File file = new File(storagePath);
        if (!file.exists()) {//如果文件夹不存在，则创建新的文件夹
            file.mkdirs();
        }
        readInputStream(storagePath + SEPARATOR + fileName, inputStream);
    }

    /**
     * 读取输入流中的数据写入输出流
     *
     * @param storagePath 目标文件路径
     * @param inputStream 输入流
     */
    public static void readInputStream(String storagePath, InputStream inputStream) {
        File file = new File(storagePath);
        try {
            if (!file.exists()) {
                // 1.建立通道对象
                FileOutputStream fos = new FileOutputStream(file);
                // 2.定义存储空间
                byte[] buffer = new byte[inputStream.available()];
                // 3.开始读文件
                int lenght = 0;
                while ((lenght = inputStream.read(buffer)) != -1) {// 循环从输入流读取buffer字节
                    // 将Buffer中的数据写到outputStream对象中
                    fos.write(buffer, 0, lenght);
                }
                fos.flush();// 刷新缓冲区
                // 4.关闭流
                fos.close();
                inputStream.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 判断文件是否存在
     *
     * @param fileName
     * @param folderPath
     * @return
     */
    public static boolean isGuideFileExist(String fileName, String folderPath) {

        File file = new File(folderPath);
        File[] array = file.listFiles();

        if (null != array && array.length > 0) {
            for (int i = 0; i < array.length; i++) {
                String name = array[i].getName();
                if (array[i].isFile() && name.endsWith(".mp4") && array[i].getName().contains(fileName)) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * 加载歌词
     *
     * @param musicTitle
     * @return
     */
    public String getLrcFilePath(String musicTitle) {

        String fileName = musicTitle + ".lrc";
        String folderPath = APPApplication.getInstance().LOCAL_PATH + "Lyric/";

        File file = new File(folderPath);
        File[] array = file.listFiles();

        if (null != array && array.length > 0) {
            for (int i = 0; i < array.length; i++) {
                if (array[i].isFile() && array[i].getName().equals(fileName)) {
                    return array[i].getPath();
                }
            }
        }

        return "";
    }

    public String formatTime(int ms) {
        int totalSeconds = ms / 1000;
        int seconds = totalSeconds % 60;
        int minutes = totalSeconds / 60 % 60;
        int hours = totalSeconds / 60 / 60;
        String timeStr = "";
        if (hours > 9) {
            timeStr += hours + ":";
        } else if (hours > 0) {
            timeStr += "0" + hours + ":";
        }
        if (minutes > 9) {
            timeStr += minutes + ":";
        } else if (minutes > 0) {
            timeStr += "0" + minutes + ":";
        } else {
            timeStr += "00:";
        }
        if (seconds > 9) {
            timeStr += seconds;
        } else if (seconds > 0) {
            timeStr += "0" + seconds;
        } else {
            timeStr += "00";
        }

        return timeStr;
    }


    public String formatDate(long seconds) {
        String finalStr = "";
        long mills = seconds * 1000;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(mills);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        finalStr += (hour < 10 ? "0" + hour : hour) + ":";
        int minute = calendar.get(Calendar.MINUTE);
        finalStr += (minute < 10 ? "0" + minute : minute) + ":";
        int second = calendar.get(Calendar.SECOND);
        finalStr += (second < 10 ? "0" + second : second);

        return finalStr;

    }
}
